<?php

App::import('Sanitize');

class AddCategoryController extends AppController {

    var $name = 'AddCategory';
    var $uses = array('Category', 'Brand', 'Categorybrand', 'Field', 'Sizelabel', 'Sizetitle', 'Sizevalue', 'Brandsetting', 'Imagetitle', 'Product', 'Label');

    function beforeFilter() {
        parent::beforeFilter();
        $this->Auth->allowedActions = array('size_labels', 'add_size_labels', 'edit_size_labels', 'delete_size_labels');
    }

    function index($id=null) {
        $this->layout = false;
        $brand_settings = $this->Brandsetting->find('all');
        if (!empty($id))
            $this->data['catID'] = $id;
        $this->data['brand_settings'] = $brand_settings;
    }

    function success() {
        $this->layout = false;
    }

    function editsuccess() {
        $this->layout = false;
    }

    function backstage_index() {
        //$this->layout = 'addcategory';
    }

    function edit($id = null) {
        $this->layout = false;
        if (!$id && empty($this->data)) {
            $this->Session->setFlash(__('Invalid Category', true));
            $this->redirect(array('action' => 'index'));
        }
        if (!empty($this->data)) {
            $this->data = Sanitize::clean($this->data, array('remove_html' => true));
            $this->data['category_name'] = trim($this->data['category_name']);
            $catid = $this->data['id'];
            $this->Category->recursive = -1;
            $category = $this->Category->read(null, $catid);
            $is_duplicate = false;
            if (isset($category['Category']['parent_id']))
                $duplicate = $this->Category->chkDuplicateCategory($this->data['category_name'], $category['Category']['parent_id'], $catid);
            else
                $duplicate = $this->Category->chkDuplicateCategory($this->data['category_name'], '', $catid);

            if ($duplicate > 0) {
                $is_duplicate = true;
            }
            if ($is_duplicate == true) {
                $this->Session->setFlash(__($this->data['category_name'] . ' Duplicate Category name. Please use different Category name.', true));
                $this->redirect(array('action' => 'success'));
            } else {
                if ($this->Category->save($this->data)) {
                    if (isset($this->data['global']) && is_array($this->data['global'])) {
                        $this->Categorybrand->recursive = -1;
                        if (isset($this->data['global']['brand'])) {
                            $this->data['global_brand'] = $this->Brand->find('all', array('conditions' => array('Brand.brand_type' => 'brand'), 'order' => 'Brand.brand_description ASC'));
                            $this->data['selected_brand'] = $this->Categorybrand->find('all', array('conditions' => array('Categorybrand.category_id' => $catid, 'Categorybrand.brand_type' => 'brand')));
                        }
                        if (isset($this->data['global']['style'])) {
                            $this->data['global_style'] = $this->Brand->find('all', array('conditions' => array('Brand.brand_type' => 'style'), 'order' => 'Brand.brand_description ASC'));
                            $this->data['selected_style'] = $this->Categorybrand->find('all', array('conditions' => array('Categorybrand.category_id' => $catid, 'Categorybrand.brand_type' => 'style')));
                        }
                        if (isset($this->data['global']['condition'])) {
                            $this->data['global_condition'] = $this->Brand->find('all', array('conditions' => array('Brand.brand_type' => 'condition'), 'order' => 'Brand.brand_description ASC'));
                            $this->data['selected_condition'] = $this->Categorybrand->find('all', array('conditions' => array('Categorybrand.category_id' => $catid, 'Categorybrand.brand_type' => 'condition')));
                        }
                        $this->data['showBrands'] = "Yes";
                    }
                } else {
                    $this->Session->setFlash(__('The category could not be saved. Please, try again.', true));
                }
            }
        }
        $brand_settings = $this->Brandsetting->find('all');

        if (empty($this->data)) {
            $this->data = $this->Category->read(null, $id);
        }
        $this->data['brand_settings'] = $brand_settings;
    }

    function add() {
        $this->layout = false;

        if (!empty($this->data)) {

            $this->data = Sanitize::clean($this->data, array('remove_html' => true));
            $this->data['category_name'] = trim($this->data['category_name']);
            $is_duplicate = false;
            if (isset($this->data['parent_id']))
                $duplicate = $this->Category->chkDuplicateCategory($this->data['category_name'], $this->data['parent_id']);
            else
                $duplicate = $this->Category->chkDuplicateCategory($this->data['category_name']);

            if ($duplicate > 0) {
                $is_duplicate = true;
            }

            if ($is_duplicate == true) {
                $this->Session->setFlash(__($this->data['category_name'] . ' Duplicate Category name. Please use different Category name.', true));
                if (isset($this->data['parent_id']))
                    $this->redirect(array('action' => 'index', $this->data['parent_id']));
                else
                    $this->redirect(array('action' => 'index'));
            }else {
                $this->Category->create();
                if ($this->Category->save($this->data)) {
                    $this->data['id'] = $this->Category->getLastInsertId();
                    if (isset($this->data['global']) && is_array($this->data['global'])) {
                        if (isset($this->data['global']['brand'])) {
                            $this->data['global_brand'] = $this->Brand->find('all', array('conditions' => array('Brand.brand_type' => 'brand')));
                        }
                        if (isset($this->data['global']['style'])) {
                            $this->data['global_style'] = $this->Brand->find('all', array('conditions' => array('Brand.brand_type' => 'style')));
                        }
                        if (isset($this->data['global']['condition'])) {
                            $this->data['global_condition'] = $this->Brand->find('all', array('conditions' => array('Brand.brand_type' => 'condition')));
                        }
                    } else {
                        $this->Session->setFlash(__('The category has been created successfully.', true));
                        $this->render('success');
                    }
                } else {
                    $this->Session->setFlash(__('The Category could not be saved. Please, try again.', true));
                    $this->render('success');
                }
            }
        }
    }

    function chkforproduct($id=null) {
        $this->layout = false;
        switch ($id) {
            case'style':$conditions = array('category_id' => $_POST['catid'], 'global_styles' => $_POST['brandid']);
                break;
            case'condition':$conditions = array('category_id' => $_POST['catid'], 'global_conditions' => $_POST['brandid']);
                break;
            case'brand':$conditions = array('category_id' => $_POST['catid'], 'global_brands' => $_POST['brandid']);
                break;
            default:$conditions = array('category_id' => $_POST['catid'], 'global_brands' => $_POST['brandid']);
                break;
        }
        $this->Product->recursive = -1;
        $total = $this->Product->find('count', array("conditions" => $conditions));
        if ($total > 0) {
            $return_arr['total'] = $total;
            $return_arr['message'] = "Yes";
        }
        else
            $return_arr['message'] = "No";
        echo json_encode($return_arr);
        $this->autoRender = false;
    }

    function editcategory() {
        $this->layout = false;
        if (!empty($this->data)) {
            $this->data = Sanitize::clean($this->data, array('remove_html' => true));
            $i = 0;
            if ($this->data['Category']['type'] == "brand") {
                $this->Categorybrand->deleteAll(array('Categorybrand.category_id' => $this->data['Category']['id'], 'Categorybrand.brand_type' => 'brand'));
                $this->Session->setFlash(__('The Global brands has been updated successfully.', true));
            }
            $cat_id = $this->data['Category']['id'];
            if (isset($this->data['global_brand'])) {
                foreach ($this->data['global_brand'] as $global_brand):
                    $this->data['Categorybrand'][$i]['brand_id'] = $global_brand;
                    $this->data['Categorybrand'][$i]['category_id'] = $cat_id;
                    $this->data['Categorybrand'][$i]['brand_type'] = "brand";
                    if (isset($this->data['updateid'][$global_brand]))
                        $this->data['Categorybrand'][$i]['id'] = $this->data['updateid'][$global_brand];
                    $i++;
                endforeach;
            }

            if (isset($this->data['global_style'])) {
                if ($this->data['Category']['type'] == "style") {
                    $this->Categorybrand->deleteAll(array('Categorybrand.category_id' => $this->data['Category']['id'], 'Categorybrand.brand_type' => 'style'));
                    $this->Session->setFlash(__('The Global styles has been updated successfully.', true));
                }
                foreach ($this->data['global_style'] as $global_style):
                    $this->data['Categorybrand'][$i]['brand_id'] = $global_style;
                    $this->data['Categorybrand'][$i]['category_id'] = $cat_id;
                    $this->data['Categorybrand'][$i]['brand_type'] = "style";
                    if (isset($this->data['updateid'][$global_style]))
                        $this->data['Categorybrand'][$i]['id'] = $this->data['updateid'][$global_style];
                    $i++;
                endforeach;
            }

            if (isset($this->data['global_condition'])) {
                if ($this->data['Category']['type'] == "condition") {
                    $this->Categorybrand->deleteAll(array('Categorybrand.category_id' => $this->data['Category']['id'], 'Categorybrand.brand_type' => 'condition'));
                    $this->Session->setFlash(__('The Global conditions has been updated successfully.', true));
                }
                foreach ($this->data['global_condition'] as $global_condition):
                    $this->data['Categorybrand'][$i]['brand_id'] = $global_condition;
                    $this->data['Categorybrand'][$i]['category_id'] = $cat_id;
                    $this->data['Categorybrand'][$i]['brand_type'] = "condition";
                    if (isset($this->data['updateid'][$global_condition]))
                        $this->data['Categorybrand'][$i]['id'] = $this->data['updateid'][$global_condition];
                    $i++;
                endforeach;
            }
            if (isset($this->data['Categorybrand'])) {
                if ($this->Categorybrand->saveAll($this->data['Categorybrand'])) {
                    $this->Session->setFlash(__('The Global brands/styles/conditions has been saved successfully.', true));
                } else {
                    $this->Session->setFlash(__('The Global brands/styles/conditions could not be saved. Please, try again.', true));
                }
            }
        }
        $this->redirect(array('action' => 'success'));
    }

    function importglobals() {
        $this->layout = false;
        if (!empty($this->data)) {
            $cat_id = $this->data['Category']['id'];
            $i = 0;
            if (isset($this->data['global_brand'])) {
                foreach ($this->data['global_brand'] as $global_brand):
                    $this->data['Categorybrand'][$i]['brand_id'] = $global_brand;
                    $this->data['Categorybrand'][$i]['category_id'] = $cat_id;
                    $this->data['Categorybrand'][$i]['brand_type'] = "brand";
                    $i++;
                endforeach;
            }

            if (isset($this->data['global_style'])) {
                foreach ($this->data['global_style'] as $global_style):
                    $this->data['Categorybrand'][$i]['brand_id'] = $global_style;
                    $this->data['Categorybrand'][$i]['category_id'] = $cat_id;
                    $this->data['Categorybrand'][$i]['brand_type'] = "style";
                    $i++;
                endforeach;
            }

            if (isset($this->data['global_condition'])) {
                foreach ($this->data['global_condition'] as $global_condition):
                    $this->data['Categorybrand'][$i]['brand_id'] = $global_condition;
                    $this->data['Categorybrand'][$i]['category_id'] = $cat_id;
                    $this->data['Categorybrand'][$i]['brand_type'] = "condition";
                    $i++;
                endforeach;
            }

            if (isset($this->data['Categorybrand'])) {
                if ($this->Categorybrand->saveAll($this->data['Categorybrand'])) {
                    $this->Session->setFlash(__('The category was created successfully and brands and conditions were imported. Global styles import was skipped.', true));
                    $this->render('success');
                }
            } else {
                $this->Session->setFlash(__('The category was created successfully', true));
                $this->render('success');
            }
        }
    }

    function editbrands($id=null, $id2=null) {
        $this->layout = false;
        if (!empty($this->data)) {
            $this->data = Sanitize::clean($this->data, array('remove_html' => true));
            if (isset($this->data['global_brand']))
                $this->data['Category']['global_brand'] = implode(",", $this->data['global_brand']);
            if (isset($this->data['global_style']))
                $this->data['Category']['global_style'] = implode(",", $this->data['global_style']);
            if (isset($this->data['global_condition']))
                $this->data['Category']['global_condition'] = implode(",", $this->data['global_condition']);

            if ($this->Category->save($this->data)) {
                $this->Session->setFlash(__('The Global brands/Style/Condition has been imported successfully', true));
                $this->redirect(array('action' => 'editbrands', $id, $id2));
            } else {
                $this->Session->setFlash(__('The user could not be saved. Please, try again.', true));
            }
        }
        $this->data = $this->Categorybrand->read(null, $id2);
        $this->data['brand_type'] = $id;
    }

    function shipping_cost($id=null) {
        $this->layout = false;
        if (!empty($this->data)) {
            $this->data = Sanitize::clean($this->data, array('remove_html' => true));
            if ($this->Category->save($this->data)) {
                $this->Session->setFlash(__('Shipping cost has been updated successfully', true));
                //$this->redirect(array('action' => 'editbrands',$id,$id2));
            } else {
                $this->Session->setFlash(__('Shipping cost could not be saved. Please, try again.', true));
            }
        }
        $this->Category->recursive = 0;
        $this->data = $this->Category->read(null, $id);
    }

    function deletesizelabel($id=null, $id2=null) {
        $this->layout = false;
        if (!empty($id)) {
            if ($this->Sizetitle->query("DELETE FROM authentic_sizetitles WHERE id='" . $id . "'")) {
                $return_arr['message'] = "Yes";
                $this->Session->setFlash(__('Size label has been deleted.', true));
            } else {
                $return_arr['message'] = "No";
                $this->Session->setFlash(__('Size label could not be deleted.', true));
            }
        }
        $this->autoRender = false;
    }

    function deletesizevalue($id=null, $id2=null) {
        $this->layout = false;
        if (!empty($id)) {
            if ($this->Sizevalue->query("DELETE FROM authentic_sizevalues WHERE id='" . $id . "'")) {
                $this->Session->setFlash(__('Size value has been deleted.', true));
            } else {
                $this->Session->setFlash(__('Size value could not be deleted.', true));
            }
        }
        $this->autoRender = false;
    }

    function update_size_title() {
        $this->layout = false;
        $this->data['id'] = $_POST['sizetitle_id'];
        $cat_id = $_POST['cat_id'];
        $this->data['size_title'] = $_POST['size_title'];
        if (!empty($this->data)) {
            $this->data = Sanitize::clean($this->data, array('remove_html' => true));
            $duplicate = $this->Sizetitle->find('count', array('conditions' => array('Sizetitle.id !=' => $this->data['id'], 'Sizetitle.category_id' => $cat_id, 'Sizetitle.size_title' => $this->data['size_title'])));
            if ($duplicate > 0) {
                $return_arr['message'] = "Duplicate";
            } else {
                if ($this->Sizetitle->save($this->data)) {
                    $return_arr['message'] = "Yes";
                    $return_arr['value'] = $this->data['size_title'];
                    $return_arr['valueID'] = $this->data['id'];
                } else {
                    $return_arr['message'] = "No";
                }
            }
        }else
            $return_arr['message'] = "No";
        echo json_encode($return_arr);
        $this->autoRender = false;
    }

    function update_size_value() {
        $this->layout = false;
        $this->data['id'] = $_POST['size_id'];
        $this->data['sizetitle_id'] = $_POST['sizetitle_id'];
        $this->data['size_value'] = $_POST['size_value'];
        if (!empty($this->data)) {
            $this->data = Sanitize::clean($this->data, array('remove_html' => true));
            $duplicate = $this->Sizevalue->find('count', array('conditions' => array('Sizevalue.id !=' => $this->data['id'], 'Sizevalue.size_value' => $this->data['size_value'], 'Sizevalue.sizetitle_id' => $this->data['sizetitle_id'])));
            if ($duplicate > 0) {
                $return_arr['message'] = "Duplicate";
            } else {
                if ($this->Sizevalue->save($this->data)) {
                    $return_arr['message'] = "Yes";
                    $return_arr['value'] = $this->data['size_value'];
                    $return_arr['valueID'] = $this->data['id'];
                } else {
                    $return_arr['message'] = "No";
                }
            }
        }else
            $return_arr['message'] = "No";
        echo json_encode($return_arr);
        $this->autoRender = false;
    }

    function addsizevalue($id=null) {
        $this->layout = false;
        if (!empty($this->data) && ($this->data['size_value'] != "Enter " . $this->data['sizetitle_size_title'] . " size")) {
            $this->data = Sanitize::clean($this->data, array('remove_html' => true));

            $duplicate = $this->Sizevalue->find('count', array('conditions' => array('Sizevalue.size_value' => $this->data['size_value'], 'Sizevalue.sizetitle_id' => $this->data['sizetitle_id'])));
            if ($duplicate > 0) {
                $return_arr['message'] = "Duplicate";
            } else {
                if ($this->Sizevalue->save($this->data)) {
                    $return_arr['message'] = "Yes";
                    $return_arr['value'] = $this->data['size_value'];
                    $return_arr['valueID'] = $this->Sizevalue->getLastInsertId();
                } else {
                    $return_arr['message'] = "No";
                }
            }
        }else
            $return_arr['message'] = "No";
        echo json_encode($return_arr);
        $this->autoRender = false;
    }

    function sizes($id=null) {
        $this->layout = false;
        if (!empty($this->data) && ($this->data['size_title'] != "Enter size title")) {
            $id = $this->data['category_id'];
            $this->data = Sanitize::clean($this->data, array('remove_html' => true));
            $this->Sizetitle->recursive = -1;
            $duplicate = $this->Sizetitle->find('count', array('conditions' => array('Sizetitle.category_id' => $this->data['category_id'], 'Sizetitle.size_title' => $this->data['size_title'])));
            if ($duplicate > 0) {
                $this->Session->setFlash(__($this->data['size_title'] . ' Duplicate Size title. Please use different.', true));
            } else {
                $this->Sizetitle->create();
                if ($this->Sizetitle->save($this->data)) {
                    $this->Session->setFlash(__('Size title has been updated successfully', true));
                } else {
                    $this->Session->setFlash(__('Size title could not be saved. Please, try again.', true));
                }
            }
        }

        $this->data['Size'] = $this->Sizetitle->find('all', array('conditions' => array('Sizetitle.category_id' => $id)));
        $this->Category->recursive = -1;
        $this->data['Cat'] = $this->Category->read(null, $id);
        if ($this->data['Cat']['Category']['sizes_dropdown'])
            $this->data['Cat']['Category']['sizes_dropdown'] = explode(',', $this->data['Cat']['Category']['sizes_dropdown']);
    }

    function size_labels($id = null) {
        $this->layout = false;
        $labels = $this->Sizelabel->find("all", array("conditions" => array("Sizelabel.category_id" => $id)));
        $this->Category->recursive = -1;
        $category = $this->Category->read(null, $id);
        $this->set(compact("category", "labels"));
    }

    function setdropdown($id = null) {
        $this->layout = false;
        $this->data['id'] = $id;
        $this->data['sizes_dropdown'] = implode(",", $_POST['setdropdown']);
        $return_arr['message'] = $id;
        if ($this->Category->save($this->data)) {
            $this->Session->setFlash(__('Sizes has been successfully set in a dropdown.', true));
        } else {
            $this->Session->setFlash(__('Sizes could not be set in a dropdown. Please, try again.', true));
        }
        echo json_encode($return_arr);
        $this->autoRender = false;
    }

    function removedropdown($id = null) {
        $this->layout = false;
        $this->data['id'] = $id;
        $this->data['sizes_dropdown'] = "";
        $return_arr['message'] = $id;
        if ($this->Category->save($this->data)) {
            $this->Session->setFlash(__('Sizes has been successfully remove from dropdown.', true));
        } else {
            $this->Session->setFlash(__('Sizes could not be remove from dropdown.', true));
        }

        if (isset($_POST['setdropdown'])) {
            $this->data['sizes_dropdown'] = implode(",", $_POST['setdropdown']);
            if ($this->Category->save($this->data)) {
                $this->Session->setFlash(__('Sizes has been successfully remove from dropdown.', true));
            } else {
                $this->Session->setFlash(__('Sizes could not be remove from dropdown.', true));
            }
        }
        echo json_encode($return_arr);
        $this->autoRender = false;
    }

    function fields($id=null) {
        $this->layout = false;
        if (!empty($this->data)) {
            $this->data = Sanitize::clean($this->data, array('remove_html' => true));
            if (substr_count($this->data['field_name'], 'defect') > 0 OR substr_count($this->data['field_name'], 'damage') > 0) {
                $this->Session->setFlash(__('You cannot add fields with word "damage" OR "defect". This field alreay exists. Please check, add product section 4.', true));
            } else {
                if (!isset($this->data['required']))
                    $this->data['required'] = "No";
                if ($this->data['field_type'] == "Checkbox")
                    if (is_array($_POST['chk_field_value']))
                        $this->data['field_value'] = implode(",", $_POST['chk_field_value']);
                    else
                        $this->data['field_value'] = trim($_POST['chk_field_value']);
                if ($this->data['field_type'] == "Dropdown")
                    if (is_array($_POST['dd_field_value']))
                        $this->data['field_value'] = implode(",", $_POST['dd_field_value']);
                    else
                        $this->data['field_value'] = trim($_POST['dd_field_value']);
                if (isset($this->data['id'])) {
                    $msg = "Additional fields(" . $this->data['field_name'] . ") has been updated successfully.";
                    $duplicate = $this->Field->find('count', array('conditions' => array('Field.id !=' => $this->data['id'], 'Field.field_name' => $this->data['field_name'], 'Field.category_id' => $this->data['category_id'])));
                } else {
                    $msg = "Additional fields has been added successfully.";
                    $duplicate = $this->Field->find('count', array('conditions' => array('Field.field_name' => $this->data['field_name'], 'Field.category_id' => $this->data['category_id'])));
                }
                if ($duplicate > 0) {
                    $this->Session->setFlash(__($this->data['field_name'] . ' duplicate Additional fields. Please use different.', true));
                } else {
                    if (isset($this->data['Label']['label_name'])) {
                        $this->data['Label']['category_id'] = $this->data['category_id'];
                        $this->Label->create();
                        $this->Label->save($this->data['Label']);
                        $this->data['label_id'] = $this->Label->getLastInsertId();
                    }
                    $this->Field->create();
                    if ($this->Field->save($this->data)) {
                        $this->Session->setFlash(__($msg, true));
                    } else {
                        $this->Session->setFlash(__('Additional fields could not be saved. Please, try again.', true));
                    }
                }
            }
        }
        $this->Category->recursive = -1;
        $category = $this->Category->read(null, $id);
        $this->Field->unbindModel(array('belongsTo' => array('Category'), 'hasOne' => array('ProductAdditionalFieldValue')));
        $fields = $this->Field->find("all", array("conditions" => array("Field.category_id" => $id), "orderby" => "Field.id asc"));
        $labels = $this->Label->find("list", array("conditions" => array("Label.category_id" => $id), "fields" => "Label.label_name"));
        $labels['0'] = "Select Label";
        $this->set(compact("category", "fields", "labels"));
    }

    function cancel_form($id=null) {
        $html = '<table width="100%" id="add_global_form" border="0" cellspacing="0" cellpadding="0"><tr><td class="alt" width="300"  valign="middle"><input type="hidden" name="data[category_id]" id="CategoryId" value="' . $id . '"><input name="data[field_name]" size="30" id="description" class="required" title="Enter field name here"></td><td class="alt" width="15"  valign="middle" style="padding:0;margin:0;"><input type="checkbox" name="data[required]" id="Save" value="Yes" /></td><td class="alt" valign="middle">Required?</td></tr><tr><td class="alt" valign="middle"><table  cellpadding="0" cellspacing="0"><tr><td class="alt">Type</td><td class="alt"><input type="radio" checked="checked" name="data[field_type]" value="Textbox" onclick="return extraFormShow(this);"></td><td class="alt">Textbox</td><td class="alt"><input type="radio" name="data[field_type]" value="Checkbox"  onclick="return extraFormShow(this);"></td><td class="alt">Checkbox</td><td class="alt"><input type="radio" name="data[field_type]" value="Dropdown"  onclick="return extraFormShow(this);"></td><td class="alt">Dropdown</td></tr></table></td><td class="alt" colspan="2"  valign="top" align="right"><input type="submit" style="background:#73AE0B; color:#fff; font-size:14px;" name="Save" id="Save" value="Save"></td></tr><tr><td colspan="3" class="alt" id="editForm"><table id="checkboxform" style="display:none;"><tr><td class="alt"><b>Values: </b><span id="checkboxfieldValue"></span><br><input name="chk_field_value" type="hidden" size="30" id="checkbox_value"><input size="30" name="checkboxoption" id="checkboxoption" title="Enter checkbox value here"><input onclick="return addCheckboxValue();" type="button" style="background:#73AE0B; color:#fff; font-size:14px;" name="Add" id="Add" value="Add Value"></td></tr></table><table id="dropdownform" style="display:none;"><tr><td class="alt">Please add "Others" with the bracket itself or copy "Others" and paste this into the last additional field. This option will allow seller to add other colors if it&#39;s not predefined.</td></tr><tr><td class="alt"><b>Values: </b><span id="spanvalue"></span><br><input name="dd_field_value" type="hidden" id="dropdown_value"><input size="30" name="dropdownoption" id="dropdownoption" title="Enter drop down value here"><input onclick="return addDropdownValue();" type="button" style="background:#73AE0B; color:#fff; font-size:14px;" name="Add" id="Add" value="Add Value"></td></tr></table></td></tr></table>';
        echo $html;
        $this->autoRender = false;
    }

    function createEditForm($id=null) {
        $this->layout = false;
        $this->Field->unbindModel(array('belongsTo' => array('Category'), 'hasOne' => array('ProductAdditionalFieldValue')));
        $this->data = $this->Field->read(null, $id);
        if ($this->data['Field']['required'] == "Yes")
            $checked = ' checked="checked"';
        else
            $checked = '';

        $checkbox_checked = "";
        $dropdown_checked = "";
        $textbox_checked = "";
        if ($this->data['Field']['field_type'] == "Checkbox")
            $checkbox_checked = ' checked="checked"';
        elseif ($this->data['Field']['field_type'] == "Dropdown")
            $dropdown_checked = ' checked="checked"';
        elseif ($this->data['Field']['field_type'] == "Textbox")
            $textbox_checked = ' checked="checked"';

        $labels = $this->Label->find("list", array("conditions" => array("Label.category_id" => $this->data['Field']['category_id']), "fields" => "Label.label_name"));
        $labels['0'] = "Select Label";
        asort($labels);

        $labelsOption = '<select id="label_id" name="data[label_id]" class="valid">';
        foreach ($labels as $key => $value) {

            if ($key == $this->data['Field']['label_id'])
                $selected = ' selected="selected"';
            else
                $selected = '';
            $labelsOption.='<option value="' . $key . '" ' . $selected . '>' . $value . '</option>';
        }
        $labelsOption.='</select>';
        $data = '<table width="100%" id="add_global_form" border="0" cellspacing="0" cellpadding="0">
              <tr><td colspan="3" class="alt">
					Select from the existing :' . $labelsOption . '
				</td></tr>
			  <tr>
                <td class="alt" width="300"  valign="middle">					
					<input type="hidden" name="data[category_id]" id="CategoryId" value="' . $this->data['Field']['category_id'] . '">
					<input name="data[field_name]" size="30" id="description" class="required" title="Enter field name here" value="' . $this->data['Field']['field_name'] . '" ' . $checked . ' />
				</td>
                <td class="alt" width="15"  valign="middle" style="padding:0;margin:0;"><input type="checkbox" name="data[required]" ' . $checked . ' id="Save" value="Yes" /></td>
				<td class="alt" valign="middle">Required?</td>
				</tr>
				<tr>
				<td class="alt" valign="middle">					
					<table  cellpadding="2" cellspacing="0"><tr><td class="alt">Type</td>
					<td class="alt">
						<input type="radio" checked="checked" name="data[field_type]" value="Textbox" ' . $textbox_checked . ' onclick="return extraFormShow(this);" />
					</td><td class="alt">Textbox</td>
					<td class="alt">
						<input type="radio" name="data[field_type]" value="Checkbox" ' . $checkbox_checked . ' onclick="return extraFormShow(this);" />
					</td><td class="alt">Checkbox</td>
					<td class="alt">
						<input type="radio" name="data[field_type]" value="Dropdown" ' . $dropdown_checked . ' onclick="return extraFormShow(this);" /></td>
					<td class="alt">Dropdown</td>
					</tr>
					</table>
				</td>
                <td class="alt" colspan="2"  valign="top" align="right"><input type="submit" style="background:#73AE0B; color:#fff; font-size:14px;" name="Save" id="Save" value="Save" /></td>
              </tr>
			  <tr>
				<td colspan="3" class="alt" id="editForm">';
        if ($this->data['Field']['field_type'] == "Checkbox")
            $style = '';
        else
            $style = ' style="display:none;"';

        $data.='<table id="checkboxform" ' . $style . '>						
						<tr><td class="alt">
						<span id="checkboxfieldValue">
						';
        //<input type="hidden" id="checkbox_value" value="'.$this->data['Field']['field_value'].'" name="data[field_value]">
        $data.='<b>Values:</b><br />';
        $field_value = explode(",", $this->data['Field']['field_value']);
        foreach ($field_value as $val):
            $data.='<input name="chk_field_value[]" type="text" size="20" value="' . trim($val) . '" ><a href="javascript:void(0);" entryID="' . $this->data['Field']['id'] . '" value="' . $val . '" onclick="return deleteCheckboxValue(this);">Delete</a><br />';
        endforeach;

        $data.='</span><input size="30" name="checkboxoption" id="checkboxoption" class="" title="Enter drop down value here" />
							<input onclick="return updateCheckboxValue(' . $this->data['Field']['id'] . ');" type="button" style="background:#73AE0B; color:#fff; font-size:14px;" name="Add" id="Add" value="Add Value" />
						</td></tr>
					</table>';

        if ($this->data['Field']['field_type'] == "Dropdown")
            $drop_style = '';
        else
            $drop_style = ' style="display:none;"';

        $data.='<table id="dropdownform" ' . $drop_style . '>
						<tr><td class="alt">Please add "Others" with the bracket itself or copy "Others" and paste this into the last additional field. This option will allow seller to add other colors if it&#39;s not predefined.</td></tr>
						<tr><td class="alt">
						<span id="fieldValue">
						';
        //<input type="hidden" id="dropdown_value" value="'.$this->data['Field']['field_value'].'" name="data[field_value]">
        $data.='<b>Values:</b><br />';
        $field_value = explode(",", $this->data['Field']['field_value']);
        foreach ($field_value as $val):
            $data.='<input name="dd_field_value[]" type="text" size="20" value="' . trim($val) . '"><a href="javascript:void(0);" entryID="' . $this->data['Field']['id'] . '" value="' . trim($val) . '" onclick="return deleteDropdownValue(this);">Delete</a><br />';
        endforeach;

        $data.='</span><input size="30" name="dropdownoption" id="dropdownoption" class="" title="Enter drop down value here" />
							<input onclick="return updateDropdownValue(' . $this->data['Field']['id'] . ');" type="button" style="background:#73AE0B; color:#fff; font-size:14px;" name="Add" id="Add" value="Add Value" />
						</td></tr>
					</table>';
        $data.='</td>
			  </tr>
            </table>';


        echo $data;
        $this->autoRender = false;
    }

    function updateDropdownValue($id=null, $id2=null) {
        $this->layout = false;
        $this->data = $this->Field->read(null, $id);
        if (!in_array(trim($id2), explode(",", $this->data['Field']['field_value']))) {
            if ($this->data['Field']['field_value'])
                $this->data['Field']['field_value'].="," . trim($id2);
            else
                $this->data['Field']['field_value'] = trim($id2);
            if ($this->Field->save($this->data)) {
                $data = '<input type="hidden"  id="dropdown_value" value="' . $this->data['Field']['field_value'] . '" name="data[field_value]"><b>Values:</b><br />';
                $field_value = explode(",", $this->data['Field']['field_value']);
                foreach ($field_value as $val):
                    $data.='<input name="dd_field_value[]" type="text" size="20" value="' . trim($val) . '" ><a href="javascript:void(0);" entryID="' . $this->data['Field']['id'] . '" value="' . trim($val) . '" onclick="return deleteDropdownValue(this);">Delete</a><br />';
                endforeach;
                echo $data;
            }
        }
        else {
            $data = '<input type="hidden"  id="dropdown_value" value="' . $this->data['Field']['field_value'] . '" name="data[field_value]"><b>Values: </b><span style="color:red;">Duplicate value.</span><br />';
            $field_value = explode(",", $this->data['Field']['field_value']);
            foreach ($field_value as $val):
                $data.='<input name="dd_field_value[]" type="text" size="20" value="' . trim($val) . '" ><a href="javascript:void(0);" entryID="' . $this->data['Field']['id'] . '" value="' . trim($val) . '" onclick="return deleteDropdownValue(this);">Delete</a><br />';
            endforeach;
            echo $data;
        }

        $this->autoRender = false;
    }

    function deleteDropdownValue($id=null, $id2=null) {
        $this->layout = false;
        $this->data = $this->Field->read(null, $id);
        $field_value = explode(",", $this->data['Field']['field_value']);
        $index = array_keys($field_value, $id2);
        unset($field_value[$index[0]]);
        $this->data['Field']['field_value'] = implode(",", $field_value);
        $this->Field->save($this->data);
        $this->data = $this->Field->read(null, $id);
        $data = '<input type="hidden"  id="dropdown_value" value="' . $this->data['Field']['field_value'] . '" name="dd_field__value"><b>Values:</b><br />';
        $field_value = explode(",", $this->data['Field']['field_value']);
        foreach ($field_value as $val):
            $data.='<input name="dd_field_value[]" type="text" size="20" value="' . trim($val) . '" ><a href="javascript:void(0);" entryID="' . $this->data['Field']['id'] . '" value="' . trim($val) . '" onclick="return deleteDropdownValue(this);">Delete</a><br />';
        endforeach;
        echo $data;
        $this->autoRender = false;
    }

    //Drop down end
    //Check box start
    function updatecheckboxvalue($id=null, $id2=null) {
        $this->layout = false;
        $this->data = $this->Field->read(null, $id);
        if (!in_array($id2, explode(",", $this->data['Field']['field_value']))) {
            if ($this->data['Field']['field_value'])
                $this->data['Field']['field_value'].="," . $id2;
            else
                $this->data['Field']['field_value'] = $id2;

            if ($this->Field->save($this->data)) {
                $data = '<b>Values:</b><br />';
                $field_value = explode(",", $this->data['Field']['field_value']);
                foreach ($field_value as $val):
                    $data.='<input name="chk_field_value[]" type="text" size="20" value="' . trim($val) . '" ><a href="javascript:void(0);" entryID="' . $this->data['Field']['id'] . '" value="' . $val . '" onclick="return deleteCheckboxValue(this);">Delete</a><br />';
                endforeach;
                echo $data;
            }
        }else {
            $data = '<b>Values: </b><span style="color:red;">Duplicate value.</span><br />';
            $field_value = explode(",", $this->data['Field']['field_value']);
            foreach ($field_value as $val):
                $data.='<input name="chk_field_value[]" type="text" size="20" value="' . trim($val) . '" ><a href="javascript:void(0);" entryID="' . $this->data['Field']['id'] . '" value="' . $val . '" onclick="return deleteCheckboxValue(this);">Delete</a><br />';
            endforeach;
            echo $data;
        }
        $this->autoRender = false;
    }

    function deletecheckboxvalue($id=null, $id2=null) {
        $this->layout = false;
        $this->data = $this->Field->read(null, $id);
        $field_value = explode(",", $this->data['Field']['field_value']);
        $index = array_keys($field_value, $id2);
        unset($field_value[$index[0]]);
        $this->data['Field']['field_value'] = implode(",", $field_value);
        $this->Field->save($this->data);
        $this->data = $this->Field->read(null, $id);
        $data = '<b>Values:</b><br />';
        $field_value = explode(",", $this->data['Field']['field_value']);
        foreach ($field_value as $val):
            $data.='<input name="chk_field_value[]" type="text" size="20" value="' . trim($val) . '" ><a href="javascript:void(0);" entryID="' . $this->data['Field']['id'] . '" value="' . $val . '" onclick="return deleteCheckboxValue(this);">Delete</a><br />';
        endforeach;
        echo $data;
        $this->autoRender = false;
    }

    //Check box end
    function editfields() {
        $this->layout = false;
        if ($this->Field->save($this->data)) {
            $return_arr['message'] = "Yes";
        } else {
            $return_arr['message'] = "No";
        }
        echo json_encode($return_arr);

        $this->autoRender = false;
    }

    function deletefield($id=null) {
        $this->layout = false;
        if ($id) {
            if ($this->Field->delete($id)) {
                $return_arr['message'] = "Yes";
            } else {
                $return_arr['message'] = "No";
            }
        }
        echo json_encode($return_arr);
        $this->autoRender = false;
    }

    function addimagetitle($id = null) {
        $this->layout = false;
        if (!empty($this->data)) {
            $this->data = Sanitize::clean($this->data, array('remove_html' => true));
            $duplicate = $this->Imagetitle->find('count', array('conditions' => array('Imagetitle.category_id' => $this->data['category_id'], 'Imagetitle.image_title' => $this->data['image_title'])));
            if ($duplicate > 0) {
                $this->Session->setFlash(__($this->data['image_title'] . ' Duplicate Image title. please use different Image title.', true));
                $this->redirect(array('action' => 'imagetitle', $id));
            } else {
                $this->Imagetitle->create();
                if ($this->Imagetitle->save($this->data)) {
                    $this->Session->setFlash(__("Image title has been saved successfully.", true));
                    $this->redirect(array('action' => 'imagetitle', $id));
                } else {
                    $this->Session->setFlash(__('Image title could not be saved. Please, try again.', true));
                }
            }
        }
    }

    function editimagetitle() {
        $this->layout = false;
        $return_arr['boxID'] = $this->data['id'];
        $return_arr['inputValue'] = $this->data['image_title'];
        $duplicate = $this->Imagetitle->find('count', array('conditions' => array('Imagetitle.id !=' => $this->data['id'], 'Imagetitle.category_id' => $this->data['category_id'], 'Imagetitle.image_title' => $this->data['image_title'])));
        if ($duplicate > 0) {
            $return_arr['message'] = "Duplicate";
        } else {

            if ($this->Imagetitle->save($this->data)) {
                $return_arr['message'] = "Yes";
            } else {
                $return_arr['message'] = "No";
            }
        }
        echo json_encode($return_arr);

        $this->autoRender = false;
    }

    function deleteimagetitle($id = null, $id2=null) {
        $this->layout = false;
        if ($this->Imagetitle->delete($id)) {

            $this->Session->setFlash(__("Image title has been deleted successfully.", true));
            $this->redirect(array('action' => 'imagetitle', $id2));
        } else {

            $this->Session->setFlash(__("Image title could not be saved. Please, try again.", true));
        }

        if ($id2) {
            $this->Imagetitle->recursive = -1;
            $this->data['Imagetitle'] = $this->Imagetitle->find('all', array(
                'conditions' => array('Imagetitle.category_id' => $id2)
                    ));
            $this->Category->recursive = 0;
            $this->data['Category'] = $this->Category->read(null, $id2);
        }
    }

    function imagetitle($id=null) {
        $this->layout = false;
        if (!empty($this->data)) {
            $this->data = Sanitize::clean($this->data, array('remove_html' => true));
            if ($this->Imagetitle->save($this->data)) {
                $this->Session->setFlash(__('Shipping cost has been updated successfully', true));
                //$this->redirect(array('action' => 'editbrands',$id,$id2));
            } else {
                $this->Session->setFlash(__('Shipping cost could not be saved. Please, try again.', true));
            }
        }
        $this->Imagetitle->recursive = -1;
        $this->data['Imagetitle'] = $this->Imagetitle->find('all', array(
            'conditions' => array('Imagetitle.category_id' => $id)
                ));
        $this->Category->recursive = 0;
        $this->data['Category'] = $this->Category->read(null, $id);
    }

    function addExisting($catid, $type) {
        $this->layout = FALSE;
        $this->data = Sanitize::clean($this->data, array('remove_html' => true));
        $this->Brand->recursive = -1;
        $brand = $this->Brand->find('all', array('conditions' => array('Brand.brand_type' => $type, 'Brand.global' => 'Yes'), 'order' => 'Brand.brand_description ASC'));
        $this->Categorybrand->recursive = -1;
        $categoryBrand = $this->Categorybrand->find('all', array('conditions' => array('Categorybrand.category_id' => $catid, 'Categorybrand.brand_type' => $type)));
        $this->set('brands', $brand);
        $this->set('categoryBrands', $categoryBrand);
        $this->set('catid', $catid);
        $this->set('type', $type);
    }

    function fetch_label_name($id=null) {
        $this->layout = false;
        if ($id == 0)
            return false;

        $this->autorender = false;
    }

    function manage_labels($id=null) {
        $this->layout = false;
        $labels = $this->Label->find("all", array("conditions" => array("Label.category_id" => $id)));
        $this->Category->recursive = -1;
        $category = $this->Category->read(null, $id);
        $this->set(compact("category", "labels"));
    }

    function add_manage_labels() {
        $this->layout = false;
        $cat_id = $this->data['category_id'];
        if (!empty($this->data) and $this->data['label_name'] != "Enter label here") {
            $duplicate = $this->Label->find('count', array('conditions' => array('Label.label_name' => $this->data['label_name'], 'Label.category_id' => $cat_id)));

            if ($duplicate > 0) {
                $this->Session->setFlash(__($this->data['label_name'] . " Duplicate label name. Please use different.", true));
                $this->redirect(array('action' => 'manage_labels', $cat_id));
            } else {
                $this->Label->create();
                if ($this->Label->save($this->data)) {
                    $this->Session->setFlash(__('Label has been saved successfully.', true));
                    $this->redirect(array('action' => 'manage_labels', $cat_id));
                } else {
                    $this->Session->setFlash(__('Label could not be saved. Please, try again.', true));
                    $this->redirect(array('action' => 'manage_labels', $cat_id));
                }
            }
        } else {
            $this->Session->setFlash(__('Enter a label name.', true));
            $this->redirect(array('action' => 'manage_labels', $cat_id));
        }
    }

    function edit_manage_labels() {
        $this->layout = false;
        $return_arr['boxID'] = $this->data['id'];
        $return_arr['brandName'] = $this->data['label_name'];
        if ($this->data['label_name'] != "") {
            $duplicate = $this->Label->find('count', array('conditions' => array('Label.id !=' => $this->data['id'], 'Label.label_name' => $this->data['label_name'], 'Label.category_id' => $this->data['category_id'])));
            if ($duplicate > 0) {
                $return_arr['message'] = "Duplicate";
            } else {
                if ($this->Label->save($this->data)) {
                    $return_arr['message'] = "Yes";
                } else {
                    $return_arr['message'] = "No";
                }
            }
        } else {
            $return_arr['message'] = "Empty";
        }
        echo json_encode($return_arr);
        $this->autoRender = false;
    }

    function delete_manage_labels($id = null, $id2=null) {
        $this->layout = false;
        if ($this->Label->delete($id)) {
            $this->Session->setFlash(__("Label has been deleted successfully.", true));
            $this->redirect(array('action' => 'manage_labels', $id2));
        } else {
            $this->Session->setFlash(__("Label could not be deleted. Please, try again.", true));
            $this->redirect(array('action' => 'manage_labels', $id2));
        }
    }

    function add_size_labels() {
        $this->layout = false;
        $cat_id = $this->data['category_id'];
        if (!empty($this->data) and $this->data['size_label'] != "Enter label here") {
            $duplicate = $this->Sizelabel->find('count', array('conditions' => array('Sizelabel.size_label' => $this->data['size_label'], 'Sizelabel.category_id' => $cat_id)));

            if ($duplicate > 0) {
                $this->Session->setFlash(__($this->data['size_label'] . " Duplicate size label name. Please use different.", true));
                $this->redirect(array('action' => 'manage_labels', $cat_id));
            } else {
                $this->Sizelabel->create();
                if ($this->Sizelabel->save($this->data)) {
                    $this->Session->setFlash(__('Size label has been saved successfully.', true));
                    $this->redirect(array('action' => 'size_labels', $cat_id));
                } else {
                    $this->Session->setFlash(__('Size label could not be saved. Please, try again.', true));
                    $this->redirect(array('action' => 'size_labels', $cat_id));
                }
            }
        } else {
            $this->Session->setFlash(__('Enter a label name.', true));
            $this->redirect(array('action' => 'size_labels', $cat_id));
        }
    }

    function edit_size_labels() {
        $this->layout = false;
        $return_arr['boxID'] = $this->data['id'];
        $return_arr['brandName'] = $this->data['size_label'];
        if ($this->data['size_label'] != "") {
            $duplicate = $this->Sizelabel->find('count', array('conditions' => array('Sizelabel.id !=' => $this->data['id'], 'Sizelabel.size_label' => $this->data['size_label'], 'Sizelabel.category_id' => $this->data['category_id'])));
            if ($duplicate > 0) {
                $return_arr['message'] = "Duplicate";
            } else {
                if ($this->Sizelabel->save($this->data)) {
                    $return_arr['message'] = "Yes";
                } else {
                    $return_arr['message'] = "No";
                }
            }
        } else {
            $return_arr['message'] = "Empty";
        }
        echo json_encode($return_arr);
        $this->autoRender = false;
    }

    function delete_size_labels($id = null, $id2=null) {
        $this->layout = false;
        if ($this->Sizelabel->delete($id)) {
            $this->Session->setFlash(__("Size label has been deleted successfully.", true));
            $this->redirect(array('action' => 'size_labels', $id2));
        } else {
            $this->Session->setFlash(__("Size label could not be deleted. Please, try again.", true));
            $this->redirect(array('action' => 'size_labels', $id2));
        }
    }

}

?>